package com.module.record.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.module.common.annotation.DataScope;
import com.module.common.utils.DateUtils;
import com.module.common.utils.StringUtils;
import com.module.common.utils.bean.BeanUtils;
import com.module.influxdb.service.InfluxDBService;
import com.module.record.domain.RecordReading;
import com.module.record.domain.vo.RecordReadingVO;
import com.module.record.mapper.RecordReadingMapper;
import com.module.record.service.IRecordReadingService;
import com.module.util.BillionResult;
import org.influxdb.InfluxDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 定时抄表记录
 */
@Service
public class RecordReadingServiceImpl extends ServiceImpl<RecordReadingMapper, RecordReadingVO> implements IRecordReadingService {

    @Autowired
    private InfluxDBService influxDBService;

    /**
     * 查询定时抄表记录
     * @param recordReadingVO
     * @return
     */
    @Override
    public List<RecordReadingVO> selectRecordReadingList(RecordReadingVO recordReadingVO)
    {
        StringBuilder queryCondition = new StringBuilder();
        queryCondition.append(" 1 = 1");
        if (StringUtils.isNotEmpty(recordReadingVO.getDeviceImei())) {
            queryCondition.append(" and device_imei =~/").append(recordReadingVO.getDeviceImei()).append("/");
        }
        if (StringUtils.isNotEmpty(recordReadingVO.getMeterNo())) {
            queryCondition.append(" and meter_no =~/").append(recordReadingVO.getMeterNo()).append("/");
        }
        if (StringUtils.isNotNull(recordReadingVO.getParams().get("beginTime"))) {
            queryCondition.append(" and time >= '").append(recordReadingVO.getParams().get("beginTime")).append(" 00:00:00'");
        }
        if (StringUtils.isNotNull(recordReadingVO.getParams().get("endTime"))) {
            queryCondition.append(" and time <= '").append(recordReadingVO.getParams().get("endTime")).append(" 23:59:59'");
        }
        List<RecordReading> readingList = influxDBService.fetchResults("device_imei, meter_no, result, result1, result2, result3, result4, result5, csq, time", "record_reading", queryCondition.toString(), "time desc", RecordReading.class);
        List<RecordReadingVO> resultList = new ArrayList<RecordReadingVO>();
        for (RecordReading recordReading : readingList) {
            RecordReadingVO readingVO = new RecordReadingVO();
            BeanUtils.copyBeanProp(readingVO, recordReading);
            readingVO.setCreateTime(DateUtils.parseUTCDate(recordReading.getTime()));
            resultList.add(readingVO);
        }
        return resultList;
    }

    /**
     * 保存定时上传记录
     * @param paramJson
     */
    @Override
    public void insertRecordReading(JSONObject paramJson) {
        JSONObject jsonObject = BillionResult.parseContent(paramJson.getString("returnOrder"));
        if(StringUtils.isNotNull(jsonObject)){
            RecordReading recordReading = new RecordReading();
            recordReading.setMeterNo(jsonObject.getString("meterNo"));
            recordReading.setDeviceImei(paramJson.getString("deviceImei"));
            recordReading.setResult(jsonObject.getDouble("result"));
            recordReading.setResult1(jsonObject.getDouble("result1"));
            recordReading.setResult2(jsonObject.getDouble("result2"));
            recordReading.setResult3(jsonObject.getDouble("result3"));
            recordReading.setResult4(jsonObject.getDouble("result4"));
            recordReading.setResult5(jsonObject.get("result5") == null ? 0 : jsonObject.getDouble("result5"));
            recordReading.setCsq(jsonObject.getString("csq"));
            recordReading.setOrder(jsonObject.getString("order"));
            influxDBService.insertOne(recordReading);
        }
    }
}
